package com.clayecloud.module_main.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.clayecloud.module_main.R;
import com.clayecloud.module_main.adapter.CardViewDatabindingAdapter;
import com.clayecloud.module_main.databinding.FragmentLifeBinding;
import com.clayecloud.module_main.viewModels.FragmentLifeViewModel;
import com.gyf.immersionbar.ImmersionBar;
import com.stx.xhb.xbanner.XBanner;
import com.stx.xhb.xbanner.transformers.Transformer;

import java.util.ArrayList;
import java.util.List;

public class LifeFragment extends Fragment {

    private FragmentLifeBinding fragmentLifeBinding;
    private FragmentLifeViewModel fragmentLifeViewModel;

    /**
     * 处理LifeFragment中的页面刷新监听等
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        fragmentLifeBinding =  DataBindingUtil.inflate(inflater, R.layout.fragment_life,container,false);
        ViewModelProvider.Factory factory = ViewModelProvider.AndroidViewModelFactory.getInstance(getActivity().getApplication());
        ViewModelProvider viewModelProvider = new ViewModelProvider(this,factory);
        fragmentLifeViewModel = viewModelProvider.get(FragmentLifeViewModel.class);

        //初始化数据
        fragmentLifeViewModel.loadData();
        fragmentLifeBinding.setModel(fragmentLifeViewModel);

        //弹性滑动
        fragmentLifeBinding.smartRefreshLayout.setEnableRefresh(false);//是否启用下拉刷新功能
        fragmentLifeBinding.smartRefreshLayout.setEnableLoadMore(false);//是否启用上拉加载功能
        fragmentLifeBinding.smartRefreshLayout.setEnablePureScrollMode(true);//是否启用纯滚动模式
        fragmentLifeBinding.smartRefreshLayout.setEnableOverScrollBounce(true);//是否启用越界回弹
        fragmentLifeBinding.smartRefreshLayout.setEnableOverScrollDrag(true);//是否启用越界拖动（仿苹果效果）1.0.4



        //轮播图
        List<String> list = new ArrayList<>();
        list.add("https://aecpm.alicdn.com/simba/img/TB15tIjGVXXXXcoapXXSutbFXXX.jpg");
        list.add("https://aecpm.alicdn.com/simba/img/TB1CWf9KpXXXXbuXpXXSutbFXXX.jpg_q50.jpg");
        fragmentLifeBinding.banner.setData(list,null);
        fragmentLifeBinding.banner.loadImage(new XBanner.XBannerAdapter() {
            @Override
            public void loadBanner(XBanner banner, Object model, View view, int position) {
                //设置图片圆角角度
                RoundedCorners roundedCorners = new RoundedCorners(40);
                //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
//                 RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(300, 300);
                RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
                Glide.with(getActivity()).load(list.get(position)).apply(options).into((ImageView) view);
            }
        });
        fragmentLifeBinding.banner.setPageTransformer(Transformer.Default);

        //横向滑动
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        fragmentLifeBinding.recyclerView.setLayoutManager(linearLayoutManager);
        CardViewDatabindingAdapter cardViewDatabindingAdapter = new CardViewDatabindingAdapter(getContext(),fragmentLifeViewModel.getLifeCards().getValue());
        fragmentLifeBinding.recyclerView.setAdapter(cardViewDatabindingAdapter);


        fragmentLifeBinding.setModel(fragmentLifeViewModel);
        return fragmentLifeBinding.getRoot();
    }



    /**
     * 用来处理创建除view的所有东西
     * @param savedInstanceState
     */
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //沉浸式状态栏
        ImmersionBar.with(this).statusBarDarkFont(true).init();
    }

    /**
     * 在删除此fragment时 进行操作防止内存泄漏
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        // 必须调用该方法，防止内存泄漏
        ImmersionBar.destroy(this);
    }




}
